/*
 * Copyright 2010 Michael Nitsche
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.mnit.basis.swt.vorlage;

import de.mnit.basis.daten.struktur.S_Folge;
import de.mnit.basis.sys.event.A_HatZuhoerer;
import de.mnit.basis.sys.event.S_Zuhoerer;

/**
 * @author Michael Nitsche
 * 21.06.2010	Erstellt
 */
public abstract class A_Zurueck<TA> extends A_HatZuhoerer<A_Zurueck<TA>> {

	private static final String E_AENDERUNG="aenderung";
//	private static final String E_KOMPLETT_ZURUECK="komplett_zurück";
	private S_Folge<TA> zurueck;
	private int pos=0;
	private boolean zurueck_single=false;


	public A_Zurueck() {
		neu();
	}


	public void neu() {
		this.zurueck=pNeu();
		pos=0;
		info();
		t.zuhoererStart(E_AENDERUNG);
	}

	public void plus() {
		info();
		if(zurueck_single)
			return;

		while(pos<zurueck.gLaenge())
			pMinusLetzten(); //zurueck.minus(-1)

		boolean ok=pPlus();
		if(ok)
			pos++;
		info();
		t.zuhoererStart(E_AENDERUNG);
	}

	public void zurueck() {
		info();
		if(pos==1 || zurueck.gLaenge()==0)
			return;
		pos--;
		iSetze();
		info();
		t.zuhoererStart(E_AENDERUNG);
//		if(pos==1) //In Planung
//			t.zuhoererStart(E_KOMPLETT_ZURUECK);
	}

	public void vor() {
		info();
		if(pos>=zurueck.gLaenge())
			return;
		pos++;
		iSetze();
		info();
		t.zuhoererStart(E_AENDERUNG);
	}

	public boolean moeglichZurueck() {
		return !(pos==1 || zurueck.gLaenge()==0);
	}

	public boolean moeglichVor() {
		return !(pos>=zurueck.gLaenge());
	}

	public void eAenderung(S_Zuhoerer<Object> z) {
		t.zuhoererPlus(E_AENDERUNG, z);
	}

//	public void eKomplettZurueck(S_Zuhoerer<?> z) {
//		t.zuhoererPlus(E_KOMPLETT_ZURUECK, z);
//	}

	// INTERN

	private void iSetze() {
		zurueck_single=true;
		pSetze(pos);
		zurueck_single=false;
	}

	private void info() {
//		Ausgabe.debug(pos, this.zurueck.gLaenge(), this.zurueck.gSpalte1());
	}

	protected abstract S_Folge<TA> pNeu();
	protected abstract void pMinusLetzten();
	protected abstract boolean pPlus();
	protected abstract void pSetze(int pos);

}
